﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BillEditorViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="BillEditorViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Bill.ViewModel
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;

    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Common.BaseClasses;
    using Hisaab.Client.Modules.Core.Properties;
    using Hisaab.Common;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="BillEditorViewModel"/> type.
    /// </summary>
    public class BillEditorViewModel : WindowViewModel, INavigationAware
    {
        /// <summary>
        /// Member varibale target manager
        /// </summary>
        private readonly IBillManager _billManager;

        /// <summary>
        /// Selected target
        /// </summary>
        private Model.Bill _selectedBill;

        /// <summary>
        /// Member variable _saveCommand
        /// </summary>
        private DelegateCommand _saveCommand;

        /// <summary>
        /// Initializes a new instance of the <see cref="BillEditorViewModel"/> class.
        /// </summary>
        public BillEditorViewModel()
            : this(ServiceLocator.Current.GetInstance<IBillManager>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BillEditorViewModel"/> class.
        /// </summary>
        /// <param name="billManager">The target Manager.</param>
        public BillEditorViewModel(IBillManager billManager)
        {
            Contract.Requires<ArgumentNullException>(billManager != null);

            _billManager = billManager;

            Title = "Bill Editor";
            Icon = new BitmapImage(new Uri("pack://application:,,,/Hisaab.Client.Common;component/Resources/Images/16/bill.png"));
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is new target.
        /// </summary>
        /// <value>
        /// True if this instance is new target; otherwise, false.
        /// </value>
        public bool IsNewTarget { get; set; }

        /// <summary>
        /// Gets the dialog type string.
        /// </summary>
        /// <value>
        /// The dialog type string.
        /// </value>
        public string DialogTypeString
        {
            get
            {
                return IsNewTarget ? Resources.Add : Resources.Edit;
            }
        }

        /// <summary>
        /// Gets or sets the selected bill.
        /// </summary>
        /// <value>The selected bill.</value>
        public Model.Bill SelectedBill
        {
            get
            {
                return _selectedBill;
            }

            set
            {
                _selectedBill = value;
                RaisePropertyChangedEvent("SelectedBill");
            }
        }

        /// <summary>
        /// Gets the save command.
        /// </summary>
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save);
                }

                return _saveCommand;
            }
        }

        #region Member Methods

        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            if (SelectedBill.Dirty && !string.IsNullOrWhiteSpace(SelectedBill.Error))
            {
                return;
            }

            if (IsNewTarget)
            {
                _billManager.Add(SelectedBill.Map<Model.Bill, Business.Entities.Bill>());
            }
            else
            {
                _billManager.Update(SelectedBill.Map<Model.Bill, Business.Entities.Bill>());
            }
        }

        #endregion Member Methods

        #region Implementation of INavigationAware

        /// <summary>
        /// Called when [navigated to].
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters["ID"] != null)
            {
                IsNewTarget = false;
                int id = int.Parse(navigationContext.Parameters["ID"]);
                SelectedBill = _billManager.GetById(id).Map<Business.Entities.Bill, Model.Bill>();
                SelectedBill.Dirty = false;
            }
            else
            {
                IsNewTarget = true;
                SelectedBill = new Model.Bill { Dirty = true, DueDate = DateTime.Today, ExpiryDate = DateTime.Today };
            }

            Title = DialogTypeString + " " + Resources.Bill;
        }

        /// <summary>
        /// Determines whether [is navigation target] [the specified navigation context].
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        /// <returns>
        ///   <c>true</c> if [is navigation target] [the specified navigation context]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        /// <summary>
        /// Called when [navigated from].
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        #endregion Implementation of INavigationAware
    }
}